package ar.edu.itba.it.paw.web.question;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.StringValidator;
import org.springframework.mail.MailAuthenticationException;

import ar.edu.itba.it.paw.domain.Product;
import ar.edu.itba.it.paw.domain.Question;
import ar.edu.itba.it.paw.domain.User;
import ar.edu.itba.it.paw.repo.UserRepo;
import ar.edu.itba.it.paw.web.MercadoLibreSession;
import ar.edu.itba.it.paw.web.base.BasePage;

public class AnswerQuestionPage extends BasePage{

	@SpringBean
	private UserRepo userRepo;

	private String answer;

	public AnswerQuestionPage(){

		

		IModel<List<Question>> questionModel = new LoadableDetachableModel<List<Question>>() {
			@Override
			protected List<Question> load() {
				MercadoLibreSession session = MercadoLibreSession.get();
				return new ArrayList<Question>(session.getUser().getUnansweredQuestions());
			}
		};

		//TODO: i need a form for each question to answer.
		add(new FeedbackPanel("feedback").add(new SimpleAttributeModifier("style", "color: #FF0000")));
		add(new PropertyListView<Question>("questionList", questionModel) {

			@Override
			protected void populateItem(ListItem<Question> item) {
				item.add(new Label("question",new PropertyModel<Question>(item.getModel(), "question")).add(new SimpleAttributeModifier("style", "color: #FFFFFF")));
				Form<Question> form = new Form<Question>("formAnswer", new CompoundPropertyModel<Question>(item.getModel())) {
					@Override
					protected void onSubmit() {
						setResponsePage(AnswerQuestionPage.class);
					}
				};
				form.add(new TextField<String>("answer").setRequired(true).add(StringValidator.maximumLength(255)));
				form.add(new Button("submit", new ResourceModel("submit")));
				item.add(form);
				item.add(new Label("date", new PropertyModel<Question>(item.getModel(), "date")).add(new SimpleAttributeModifier("style", "")));
			}
			
		});
	}
}
